<?php
declare(strict_types=1);

use Zms\Validate\Type;
use Zms\Validate\ValidateAbstract;

include "../vendor/autoload.php";

class Validate extends ValidateAbstract
{
    public function rules(): array
    {
        return [
            ['user_id', Type::REQUIRED_CHOOSE],
            ['accepted', Type::ACCEPTED],
            ['domain', Type::DOMAIN, 'clear_ending' => true],//敏感词检测
            ['user.mobile', Type::MOBILE],//敏感词检测
            ['violate', Type::VIOLATE, 'words' => ['沙壁'], 'model' => 'replace'],//敏感词检测
            ['point', Type::POINT],//铭感词检测

            ['callback', 'callbackValidate'],

            ['function', function ($value, $find, $data) {
                if ($value) {
                    return $value;
                }
                return $this->addError($find, '{label}不能小于1');
            }],
        ];
    }

    public function labels(): array
    {
        return [
            'user_id' => '注册会员',
            'accepted' => '注册协议',
            'domain' => '域名',
            'user.mobile' => '用户手机',
            'violate' => '敏感词',
            'point' => '位置',
            'callback' => '回调',
            'function' => '函数'
        ];
    }

    /**
     * @param mixed $value
     * @param string $find
     * @param array $data
     * @return null
     */
    public function callbackValidate($value, $find, $data)
    {
        return $value ?: null;
    }
}


$validate = (new Validate())
    ->setData([
        'user_id2' => 1,
        'accepted' => 1,
        'domain' => 'https://127.0.0.1',
        'user' => [
            'mobile' => '18676405902'
        ],
        'violate' => [
            'name' => '1张三李四王五',
            'info' => [
                '3张三李沙壁四王五',
                '4这种方法不仅沙壁的可读性和可维护性',
            ]
        ],
        'point' => [
            "lng" => "121.498046", //经度
            "lat" => "31.221615" //纬度
        ],
        'callback' => [1, 2, 3, 4],
        'function' => 1
    ])
    ->validate();

if ($validate->isFail()) {//验证失败
    print_r([
        'error' => $validate->getErrorList()
    ]);
} else {//验证成功
    print_r([
        'data' => $validate->getSafeData()
    ]);
}
